<?php
/**
 * Created by JetBrains PhpStorm.
 * User: oliverson
 * Date: 12/3/12
 * Time: 8:59 PM
 * To change this template use File | Settings | File Templates.
 */
class AutoLoader_EMyLink
{
    public static function autoLoader($file_name)
    {
        if(file_exists(ROOT_PATH."/library/EMyLink/".$file_name."/".$file_name."_EMyLink.php"))
        {
            require_once("/library/EMyLink/".$file_name."/".$file_name."_EMyLink.php");
        }
    }
    public static function modelLoader($name_model)
    {
        if(file_exists(ROOT_PATH."/library/EMyLink/Model/".$name_model.".php"))
        {
            require_once("/library/EMyLink/Model/Root/root_".$name_model.".php");
            require_once("/library/EMyLink/Model/".$name_model.".php");
        }
    }
    public static function formLoader($name_form)
    {
        if(file_exists(ROOT_PATH."/library/EMyLink/Form/".$name_form.".php"))
        {
            require_once("/library/EMyLink/Form/Root/root_".$name_form.".php");
            require_once("/library/EMyLink/Form/".$name_form.".php");
        }
    }
    public static function writeLog($msg, $file="")
    {
        $server_path=ROOT_PATH."/";
        if(empty($file))
            $filepath = $server_path . 'log/log-'.date('Y-m-d').".php";
        else
            $filepath = $file;
        $message  = '';

        if ( ! file_exists($filepath))
        {
            $message .= "<"."?php  if ( ! defined('IN_TMO')) exit('No direct script access allowed'); ?".">\n\n";
        }

        @chmod($filepath, 0777);

        if ( ! $fp = @fopen($filepath, "ab"))
        {
            return FALSE;
        }

        if(!is_string($msg)) $msg = print_r($msg, true);

        $message .= date('Y-m-d; H:j'). " --> ". $msg ."\n";

        flock($fp, LOCK_EX);
        fwrite($fp, $message);
        flock($fp, LOCK_UN);
        fclose($fp);
        @chmod($filepath, 0666);
        return TRUE;
    }
    /*
     * lấy dữ liệu file ini
     */
    public static function getFileIni($file,$name,$flag=1)
    {
        if(file_exists($file))
        {
            $array = parse_ini_file($file,$flag);
            if(isset($array[$name]))
            {
                return $array[$name];
            }
        }
        return "";
    }
    /*
     * Kiểm tra giá trị trong mang nếu đúng trả ra giá trị , sai trả về giá trị người dùng đưa vào
     */
    public static function checkValueArray($name,$value="",$arr)
    {
        if(isset($arr[$name]))
        {
            return $arr[$name];
        }
        else
        {
            return $value;
        }
    }
}
